package com.centaline.androidsalesblog.fragments.map;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.base.PostMainMapFragment;
import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.constants.CommonConstant;
import com.centaline.androidsalesblog.model.businissobject.DropBo;
import com.centaline.androidsalesblog.rx.database.DropSaleRegionFunc1;
import com.centaline.androidsalesblog.rx.database.MetroFunc1;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centaline.androidsalesblog.rx.database.SearchDataFunc1;
import com.centaline.androidsalesblog.widgets.dropdown.SalePriceDrop;
import com.centaline.androidsalesblog.widgets.dropdown.SingleDrop;
import com.centaline.androidsalesblog.widgets.dropdown.TrebleDrop;
import com.trello.rxlifecycle.FragmentEvent;

import java.util.ArrayList;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func3;
import rx.schedulers.Schedulers;

/**
 * 二手房地图找房
 * Created by guilin on 16/4/14.
 */
public class RentMainMapFragment extends PostMainMapFragment {

    private TrebleDrop trebleDrop;
    private SalePriceDrop salePriceDrop;
    private SingleDrop areaDrop;
    private SingleDrop roomDrop;

    @Override
    protected String getPostType() {
        return "r";
    }

    @Override
    protected int dropArrayId() {
        return R.array.mainMapRent;
    }

    @Override
    protected void initDrop() {
        super.initDrop();
        trebleDrop = new TrebleDrop(dropDownView, getActivity());
        salePriceDrop = new SalePriceDrop(dropDownView, getActivity());
        roomDrop = new SingleDrop(dropDownView, getActivity());
        areaDrop = new SingleDrop(dropDownView, getActivity());
        dropDownView.addDrops(trebleDrop, salePriceDrop, roomDrop, areaDrop);

        Observable.combineLatest(RxDataBase.getSaleRegions()
                        .subscribeOn(Schedulers.io())
                        .map(new DropSaleRegionFunc1()),
                RxDataBase.nearby(),
                RxDataBase.getMetro()
                        .subscribeOn(Schedulers.io())
                        .map(new MetroFunc1()),
                new Func3<ArrayList<DropBo>, DropBo, ArrayList<DropBo>, ArrayList<DropBo>>() {
                    @Override
                    public ArrayList<DropBo> call(ArrayList<DropBo> dropBos, DropBo nearBy, ArrayList<DropBo> dropBos3) {
                        final ArrayList<DropBo> list = new ArrayList<>();
                        DropBo dropBo;
                        if (dropBos.size() > 0) {
                            dropBo = new DropBo(10);
                            dropBo.setText("区域");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(new ArrayList<DropBo>());
                            DropBo dropBo1 = new DropBo(10);
                            dropBo1.setText(CommonConstant.DROP_TEXT_NO_LIMITED);
                            dropBo1.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.getChildrenList().add(dropBo1);
                            if (AppConstant.NearbyEnable) {
                                dropBo.getChildrenList().add(nearBy);
                            }
                            dropBo.getChildrenList().addAll(dropBos);
                            list.add(dropBo);
                        }
                        if (dropBos3.size() > 0) {
                            dropBo = new DropBo(20);
                            dropBo.setText("地铁");
                            dropBo.setValue(CommonConstant.DROP_VALUE_NULL);
                            dropBo.setChildrenList(dropBos3);
                            list.add(dropBo);
                        }
                        return list;
                    }
                }).compose(this.<ArrayList<DropBo>>bindUntilEvent(FragmentEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        trebleDrop.init(dropBos);
                    }
                });
        RxDataBase.getSearchData("Rent")
                .map(new SearchDataFunc1(30))
                .subscribeOn(Schedulers.io())
                .compose(this.<ArrayList<DropBo>>bindUntilEvent(FragmentEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        salePriceDrop.init(dropBos);
                    }
                });
        RxDataBase.getSearchData("Room")
                .map(new SearchDataFunc1(40))
                .subscribeOn(Schedulers.io())
                .compose(this.<ArrayList<DropBo>>bindUntilEvent(FragmentEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        roomDrop.init(dropBos);
                    }
                });
        RxDataBase.getSearchData("Area")
                .map(new SearchDataFunc1(60))
                .subscribeOn(Schedulers.io())
                .compose(this.<ArrayList<DropBo>>bindUntilEvent(FragmentEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<DropBo>>() {
                    @Override
                    public void call(ArrayList<DropBo> dropBos) {
                        areaDrop.init(dropBos);
                    }
                });
    }
}
